package com.ctos.oj.judge.codeSandBox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.ctos.oj.common.ErrorCode;
import com.ctos.oj.exception.BusinessException;
import com.ctos.oj.judge.codeSandBox.CodeSandBox;
import com.ctos.oj.judge.codeSandBox.model.ExecuteCodeRequest;
import com.ctos.oj.judge.codeSandBox.model.ExecuteCodeResponse;

/**
 * 远程代码沙箱 (调用接口)
 */
public class RemoteCodeSandBox implements CodeSandBox {
    private static final String AUTH_HEADER = "AUTH";

    private static final String AUTH_SECRET = "ctos";

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCoderequest) {
        if (executeCoderequest == null){
            throw new  BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        String url = "http://192.168.139.133:8090/executeCode";
        String responseStr = HttpUtil
                .createPost(url)
                .header(AUTH_HEADER, AUTH_SECRET)
                .body(JSONUtil.toJsonStr(executeCoderequest))
                .execute()
                .body();
        ExecuteCodeResponse executeCodeResponse = JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
        if (executeCodeResponse == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        return executeCodeResponse;
    }
}
